/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.dialogs;


import java.util.Iterator;

import lombok.Getter;
import lombok.Setter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.google.code.cubeirc.common.Utils;
import com.google.code.cubeirc.editor.Customs;



/**
 * Show colors on Styles text when press CTRL+W
 * @author squid
 *
 */
public class ColorsChooser extends Dialog {

	protected String result = "";
	protected Shell shell;
	@Getter @Setter
	private Control Sender;
	
	@Getter @Setter
	private Customs colors;

	private ToolBar toolBar;
	
	public ColorsChooser(Shell parent, int style, Control sender) {
		super(parent, style);
		setSender(sender);
	}

	private void setPosition()
	{
		Point pt = getSender().toDisplay(0,0);
		StyledText st = (StyledText) getSender();		
		this.shell.setLocation(pt.x+ st.getCaret().getLocation().x,pt.y-shell.getSize().y - 2);
	}

	public Object open() {
		jbInit();
		this.shell.open();
		this.shell.layout();				
		Display display = getParent().getDisplay();
		setPosition();
		
		while (!this.shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return this.result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void jbInit() {
		this.shell = new Shell(getParent(), SWT.NONE);
		this.shell.setSize(234, 50);
		this.shell.setText(getText());
		this.shell.setLayout(null);
		
		this.toolBar = new ToolBar(this.shell, SWT.FLAT | SWT.WRAP | SWT.RIGHT);
		this.toolBar.setBounds(0, 0, 232, 48);
					
		initButtons();
		
		
	}
	
	private void initButtons()
	{
		Utils.asyncExec(shell.getDisplay(), new Runnable() {
			
			@Override
			public void run() {
				setColors(new Customs());		
				for (Iterator<String> it = getColors().colors.keySet().iterator(); it.hasNext();)
				{
					String key = it.next();
					Color value = getColors().colors.get(key);
					
					final ToolItem bet = new ToolItem(toolBar, SWT.FLAT);					
					bet.setImage(getCheckedImage(shell.getDisplay(), value));
					bet.setToolTipText(key);
					bet.setData(String.format("[color_%s]", bet.getToolTipText()));
					bet.addSelectionListener(new SelectionAdapter() {
						@Override
						public void widgetSelected(SelectionEvent arg0) {
						     result =  bet.getData().toString();
						     shell.dispose();
							super.widgetSelected(arg0);
						}
					});
				}												
			}
		});
		
	}
	
	static Image getCheckedImage(Display display, Color color) {
		Image image = new Image(display, 16, 16);
	    GC gc = new GC(image);
	    gc.setBackground(color);
	    gc.fillRectangle(0, 0, 16, 16);
	    gc.dispose();
	    return image;
	  }
	
}
